<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 系统管理类
 *
 * @package        	CodeIgniter
 * @subpackage    	Controller
 * @category    	Controller
 */

class SystemManage extends MY_Controller{

	function __construct(){
		parent::__construct();
	}
	
//用户管理开始========================================================================
	
	//取用户列表页模版
	function userlist(){
		if($this->acl->check_privilege('user_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$this->load->view('rbac/user_list');
		}
	}
	
	//取用户列表页json数据列表
	function _user_data_post(){
		$rowdata = array();
		$dept_id = $this->uri->segment(3,0);
		$row = $this->user_m->userlist($dept_id);
		foreach ($row as $v){
			$rowrole = $this->user_m->rolelist($v->role_id);
			$rowdept = $this->user_m->deptlist($v->dept_id);
			$rowdata[] = array("id"=>$v->user_id,"user_name"=>$v->user_name,"user_actual"=>$v->user_actual,"user_aliases"=>$v->user_aliases,"user_pass"=>$v->user_pass,"role_id"=>$v->role_id,"role_name"=>$rowrole[0]->role_name,"user_sort"=>$v->user_sort,"dept_id"=>$v->dept_id,"dept_name"=>$rowdept[0]['text'],"user_disable"=>$v->active);
		}
		$userlistdata = array("total"=>count($row),'rows'=>$rowdata);
		echo json_encode($userlistdata); 
	}
	
	//新建用户
	function _user_add_post(){
		if($this->acl->check_privilege('user_add') == false){
			$this->rejsonsuccess($data,"您没有新建用户的权限");
		}else{
			$data= $this->input->post();
			if($this->user_m->identity_check($data['user_name'])){
				$msg = '用户名重复';
				$data = false;
			}else if($this->user_m->aliases_check($data['user_name'],$data['user_aliases'])){
				$msg = '别名重复';
				$data = false;
			}else{
				$msg = '错误';
				$data = $this->user_m->useradd($data);
			}
			$this->rejsonsuccess($data,$msg);

		}
	}
	
	//编辑用户
	function _user_edit_post(){
		$id = $this->uri->segment(3,0);
		$arr = $this->input->post();
		if($this->user_m->aliases_check($arr['user_name'],$arr['user_aliases'])){
			$msg = '别名重复';
			$data = false;
		}else{
			$msg = '未知错误';
			$data = $this->user_m->useredit($id,$arr);
		}
		$this->rejsonsuccess($data,$msg);
	}
	
	//删除用户
	function _user_delete_post(){
		if($this->acl->check_privilege('user_delete') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$arr = $this->input->post();
			//$this->load->model('user_m');
			$data = $this->user_m->userdel($arr['id']);
			$this->rejsonsuccess($data,"删除用户失败，请联系系统管理员");
		}
	}


//节点管理开始========================================================================
	//节点列表视图
	function menulist(){
		if($this->acl->check_privilege('menu_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$this->load->view('rbac/menu_list');
		}
	}
	
	//节点列表数据
	function _menu_data_post(){
		if($this->acl->check_privilege('privilege_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$role_id = $this->uri->segment(3,0);
			$roleprivilege = $this->user_m->roleprivilege($role_id);
			$menuprivilege = $this->acl->get_tree($this->user_m->menulist('',$roleprivilege));
			echo json_encode($menuprivilege); 
		}
	}
	
	//节点编辑视图
	function menuedit(){
		$arr = array();
		$menu_id = $this->uri->segment(3,0);
		$data['menuinfo'] = $this->user_m->menulist($menu_id,$arr);
		$data['menuall'] = $this->user_m->menuall();
		$this->load->view('rbac/menu_edit',$data);
	}
	
	//节点编辑提交
	function _menu_edit_post(){
		$id = $this->uri->segment(3,0);
		$arr = $this->input->post();
		$data = $this->user_m->menuedit($arr,$id);
		$this->rejsonsuccess($data,"修改节点失败，请联系系统管理员");
	}
	
	//节点添加提交
	function _menu_add_post(){
		$arr = $this->input->post();
		$data = $this->user_m->menuadd($arr);
		if($data){
			echo json_encode(array("success"=>true,"id"=>$data['id'],"pid"=>$data['pid']));
		}else{
			echo json_encode(array("success"=>false,"errorMsg"=>"新建菜单失败，请联系系统管理员"));	
		}
	}
	
	//节点删除
	function _menu_del_post(){
		if($this->acl->check_privilege('menu_delete') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$arr = $this->input->post();
			$data = $this->user_m->menudel($arr);
			$this->rejsonsuccess($data,"删除节点失败，请联系系统管理员");
		}
	}
	

//部门管理开始========================================================================

	//部门管理视图
	function deptlist(){
		if($this->acl->check_privilege('dept_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$this->load->view('rbac/dept_list');
		}
	}
	
	//部门数据
	function _dept_data_post(){	
		$depttree = $this->acl->get_tree($this->user_m->deptlist(''));
		echo json_encode($depttree); 
	}
	
	//部门添加
	function _dept_add_post(){
		$arr = $this->input->post();
		$data = $this->user_m->deptadd($arr);
		if($data){
			echo json_encode(array("success"=>true,"id"=>$data['id'],"pid"=>$data['pid']));
		}else{
			echo json_encode(array("success"=>false,"errorMsg"=>"新建部门失败，请联系系统管理员"));	
		}
	}

	//部门编辑视图
	function deptedit(){
		$arr = array();
		$dept_id = $this->uri->segment(3,0);
		$data['deptinfo'] = $this->user_m->deptlist($dept_id,$arr);
		$this->load->view('rbac/dept_edit',$data);
	}
	
	
	//部门编辑提交
	function _dept_edit_post(){
		$id = $this->uri->segment(3,0);
		$arr = $this->input->post();
		$data = $this->user_m->deptedit($arr,$id);
		$this->rejsonsuccess($data,"编辑节点失败，请检查该部门下是否有用户!");
	}
	
	//部门删除
	function _dept_del_post(){
		if($this->acl->check_privilege('dept_delete') == false){
			$this->rejsonsuccess(false,"删除部门失败，没有权限!");
		}else{
			$arr = $this->input->post();
			$data = $this->user_m->deptdel($arr);
			$this->rejsonsuccess($data,"删除部门失败，请检查该部门下是否有用户!");
		}
	}
	

//角色管理========================================================================
	//角色管理视图
	function rolelist(){
		if($this->acl->check_privilege('role_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$this->load->view('rbac/role_list');
		}
	}
	
	//角色管理数据列表
	function _role_data_post(){
		if($this->acl->check_privilege('role_list') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rolelistdata = $this->user_m->rolelist();
			if (count($rolelistdata) > 0){
				foreach ($rolelistdata as $v){
					$rowdata[] = array("id"=>$v->role_id,"role_name"=>$v->role_name,"role_groupname"=>$v->role_groupname,"role_sort"=>$v->role_sort);
				}
				$rolelistdata = array("total"=>count($rolelistdata),'rows'=>$rowdata);
				echo json_encode($rolelistdata); 
			}			
		}
	}

	//角色管理新建
	function _role_add_post(){
		$arr = $this->input->post();
		$this->load->model('user_m');
		$data = $this->user_m->roleadd($arr);
		$this->rejsonsuccess($data,"新建角色失败，请联系系统管理员");
	}
	
	//角色编辑
	function _role_edit_post(){
		$id = $this->uri->segment(3,0);
		$arr = $this->input->post();
		$data = $this->user_m->roleedit($id,$arr);
		$this->rejsonsuccess($data,"编辑失败，请联系系统管理员");
	}
	
	//角色列表(JSON格式)
	function _role_data_select_post(){
		$rolelistdata = $this->user_m->rolelist();
		if (count($rolelistdata) > 0){
			$id = $this->uri->segment(3, 0);
			foreach ($rolelistdata as $v){
				if($id == $v->role_id){
					$rowdata[] = array("id"=>$v->role_id,"text"=>$v->role_name,'selected'=>true);
				}else{
					$rowdata[] = array("id"=>$v->role_id,"text"=>$v->role_name);
				}
			}
			echo json_encode($rowdata); 
		}
	}

	//角色删除
	function _role_del_post(){
		if($this->acl->check_privilege('role_delete') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$arr = $this->input->post();
			$data = $this->user_m->roledel($arr['id']);
			$this->rejsonsuccess($data,"该角色下有用户：要删除该角色，需要先删除或修改该角色下的用户");
		}
	}
	
	//角色权限更新
	function _role_privilege_edit_post(){
		$arr = $this->input->post();
		$data = $this->user_m->privilegeeditpost($arr);
		$this->rejsonsuccess($data,"权限更新错误");
	}
	
	
//站点设置========================================================================
	//站点设置视图
	function settingssite(){
		if($this->acl->check_privilege('settings_site') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$data['site'] = $this->user_m->settings_site();
			$this->load->view('rbac/settings_site',$data);
		}
	}
	
	//站点设置数据提交
	function _settingssite_post(){
		if($this->acl->check_privilege('settings_site') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$arr = $this->input->post();
			$result = $this->user_m->settings_site($arr);
			$this->rejsonsuccess($result,'修改失败!');

		}
	}
	
//个人信息修改========================================================================
	//个人设置视图
	function personal_info(){
		$data['user_info'] = $this->user_m->get_personal_user_info();
		$this->load->view('rbac/personal_info',$data);
	}
	
	//个人设置数据提交
	function _personal_info_post(){
		$data=$this->input->post();
		if(!empty($data)){
			$user_name = $this->session->userdata('user_name');
			if($this->user_m->aliases_check($user_name,$data['personal_user_aliases'])){
				$msg = '别名重复';
				$result = false;
			}else{
				$msg = '未知错误';
				$result = $this->user_m->set_personal_user_info($data);
			}
			$this->rejsonsuccess($result,$msg);	
		}
	}

	//修改密码视图
	function set_personal_user_pwd(){
		$data['userinfo'] = $this->user_m->userlist(false,$this->session->userdata('user_id'));
		$this->load->view('rbac/user_passedit',$data);
	}
	
	//修改密码数据提交
	function _set_personal_user_pwd_post(){
		$arr = $this->input->post();
		if(!empty($arr)){
			$identity = $this->session->userdata($this->config->item('identity', 'yjoa_config'));
			$this->rejsonsuccess($this->user_m->user_passedit($identity, $arr['personal_pwd_old'], $arr['personal_pwd_new']),'旧密码错误，请核对后重新输入!');
		}
	}

}